package com.olympia.myapp.custom;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.olympia.myapp.R;

public class Dialog_Login extends Dialog implements OnClickListener {
	Activity activity;
	ProcessDialogLogin process;
	TextView txtRegis;
	EditText edtMail;
	EditText edtPass;
	Button btnLogin;
	ImageView btnClose;
	public ProgressBar progress;

	/**
	 * 
	 * @param context
	 */
	public Dialog_Login(Context context, ProcessDialogLogin process) {
		super(context);
		activity = (Activity) context;
		/** 'Window.FEATURE_NO_TITLE' - Used to hide the title */
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		/** Design the dialog in main.xml file */
		setContentView(R.layout.dialog_login);
		this.process = process;

		progress = (ProgressBar) findViewById(R.id.progress_dg);
		btnClose = (ImageView) findViewById(R.id.dg_iconClose);
		btnLogin = (Button) findViewById(R.id.dg_btnLogin);
		edtMail = (EditText) findViewById(R.id.edt_mail);
		edtPass = (EditText) findViewById(R.id.edt_pass);
		txtRegis = (TextView) findViewById(R.id.txtRegis);
		btnLogin.setOnClickListener(this);
		btnClose.setOnClickListener(this);
		txtRegis.setOnClickListener(this);
	}

	public void setText(String mail, String pas) {
		edtMail.setText(mail);
		edtPass.setText(pas);
	}

	@Override
	public void onClick(View v) {
		if (v == btnClose) {
			dismiss();
		} else if (v == btnLogin) {
			String mail = edtMail.getText().toString();
			String pass = edtPass.getText().toString();

			if (validate(mail, pass)) {
				progress.setVisibility(View.VISIBLE);
				process.click_Ok(mail, pass);
			} else {
				if (mail.length() <= 0)
					edtMail.setError("Email đăng nhập không tồn tại, xin vui lòng kiểm tra lại!");
				else if (pass.length() <= 0)
					edtPass.setError("Mật khẩu đăng nhập không đúng, xin vui lòng kiểm tra lại!");
			}
		} else if (v == txtRegis) {
			dismiss();
			process.click_Regis();
		}
	}

	public void hideBtnRegis() {
		txtRegis.setVisibility(View.GONE);
	}

	private boolean validate(String mail, String pass) {
		return mail.length() > 0 && pass.length() > 0;
	}

	public static abstract class ProcessDialogLogin {
		public abstract void click_Ok(String mail, String pass);

		public abstract void click_Regis();

	}
}
